package com.client.rule;

import java.util.Map;

import com.client.rule.triple.TripleParser;
import com.client.rule.triple.UiComplexTriple;
import com.client.rule.triple.UiSimpleTriple;
import com.client.rule.triple.UiTriple;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.xml.client.Element;
import com.google.gwt.xml.client.Node;
import com.shared.rule.TypeComplexTriple;
import com.shared.rule.TypeDataRule;

public class UiRuleComposite extends UiRule{
	
	private UiTriple triple;
	HorizontalPanel panel = new HorizontalPanel();	
	private TextBox textBox = new TextBox();
	private Button saveButton = new Button("Sauver");
	



	public UiRuleComposite() {
		super();
		label = "Nouvelle condition";		
		textBox.addChangeHandler(new ChangeHandler() {			
			//@Override
			public void onChange(ChangeEvent event) {				
					triple = null;	
			}
		});
		saveButton.addClickHandler(new ClickHandler(){
		public void onClick(ClickEvent e) {
		        if (!textBox.getText().isEmpty()) {		        			        		
		        		UiRuleComposite.this.setText(textBox.getText());
		        		triple = null;	
		        	
		        }
		      }
		 });
		textBox.setText(label);
		panel.add(textBox);
		panel.add(saveButton);
		setWidget(panel);		
	}
	
			
	
	public void setText(String text){
		label = text;		
		super.setText(label);		
	}
	
	public void setText(){
		label = textBox.getValue();		
		super.setText(label);		
	}
	
	public void showWidget(){
		setWidget(panel);		
	}
	
		
	
	
	
	public UiRuleComposite getComplement() {
		UiRuleComposite uiRule = new UiRuleComposite();
		uiRule.label = "NOT("+getLabel()+")";	
		uiRule.textBox.setValue(uiRule.label);
		
		if(triple!=null){
			UiComplexTriple tripleTemp = new UiComplexTriple("ComplementTriple",TypeComplexTriple.NOT);
			UiTriple t = getTriple();
			tripleTemp.add(t);
			uiRule.setTriple(tripleTemp);
		}
		
		return uiRule;
	}
	
		
	public void generateFromXML(Node xmlNode){
		
		Element ruleElement = (Element) xmlNode;		
		setLabel(TripleParser.replaceForbiddenChar(ruleElement.getAttribute("label")));		
		textBox.setValue(label);
		
		
		setText(label);
		int i = 1;			
		for (Node n = xmlNode.getFirstChild(); n != null; n = n.getNextSibling())	{	
			Element rElement = (Element) n;		
			
			
			if(i==1){	
				
				if(TypeComplexTriple.isComplex(rElement.getAttribute("type"))){	
					
					triple = new UiComplexTriple();					
				}else{
					
					triple = new UiSimpleTriple();					
				}
				triple.generateFromXML(n);
			}else{
				if(rElement.getAttribute("type").equals("Composite")){
					UiRule treeItem = new UiRuleComposite();				
					treeItem.generateFromXML(n);	
					addItem(treeItem);	
				}else{
					
					UiRule treeItem = new UiDecision();
					treeItem.generateFromXML(n);	
					addItem(treeItem);	
				}
			}
			i++;		
		}		
	}

	@Override
	public boolean isComposite() {		
		return true;
	}
	
	public boolean hasDecision(){
		if(getChildCount()!=1) return false;
		if(((UiRule) getChild(0)).isComposite()) return false;
		return true;
	}

	@Override
	public String toXml(Map<String,TypeDataRule> mapData) throws IllegalArgumentException{
		String xml = "";			
		if(triple==null) triple=TripleParser.parse(label, mapData);	
		
		xml = "<Rule type=\"Composite\" label=\""+TripleParser.changeForbiddenChar(label)+"\">"+triple.toXml(mapData);		
		for(int i=0;i<getChildCount();i++) xml=xml+((UiRule) getChild(i)).toXml(mapData);		
		xml = xml+"</Rule>";
		return xml;
	}



	@Override
	public void getUsedDataXml(Map<String, String> usedData,Map<String,TypeDataRule> mapData) throws IllegalArgumentException {				
		if(triple==null) triple=TripleParser.parse(label, mapData);
		//Window.alert("Composte getUsedDataXml"+triple.toXml(mapData));
		triple.getUsedDataXml(usedData,mapData);
		for(int i=0;i<getChildCount();i++) ((UiRule) getChild(i)).getUsedDataXml(usedData,mapData);
	}

	public TextBox getTextBox() {
		return textBox;
	}
	
	/**
	 * @return the triple
	 */
	public UiTriple getTriple() {
		return triple;
	}



	/**
	 * @param triple the triple to set
	 */
	public void setTriple(UiTriple triple) {
		this.triple = triple;
	}

	
}
